package com.example.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.crypto.SecureUtil;
import com.example.common.ResultCode;
import com.example.dao.SigninusersInfoDao;
import com.example.entity.*;
import com.example.entity.SigninusersInfo;
import com.example.exception.CustomException;
import com.example.vo.SigninusersInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class SigninusersInfoService {

    @Resource
    private SigninusersInfoDao signinusersInfoDao;
	
	
	//kuabiaojisuan3

    public SigninusersInfo add(SigninusersInfo signinusersInfo) {
        List<Long> picflst = signinusersInfo.getPicflst();
        if (!CollectionUtil.isEmpty(picflst)) {
            signinusersInfo.setPic(picflst.toString());
        }
		
		        //shangxchxuantupxian
        // 唯一校验
        int count = signinusersInfoDao.checkRepeat("username", signinusersInfo.getUsername(), null);
if (count > 0) {
	throw new CustomException("1001", "该账号\"" + signinusersInfo.getUsername() + "\"已存在");
}
      // signinusersInfo.setAccount((double) 0);
	  
        signinusersInfoDao.insertSelective(signinusersInfo);
        return signinusersInfo;
    }
	
	public List<Map<String,String>> findByDiqu() {
        return signinusersInfoDao.findByDiqu();
    }

    public void delete(Long id) {
        signinusersInfoDao.deleteByPrimaryKey(id);
    }

    public void update(SigninusersInfo signinusersInfo) {
        List<Long> picflst = signinusersInfo.getPicflst();
        if (!CollectionUtil.isEmpty(picflst)) {
            signinusersInfo.setPic(picflst.toString());
        }
		
		        //shangxchxuantupxian
		//youdianzan
        signinusersInfoDao.updateByPrimaryKeySelective(signinusersInfo);
    }

    public SigninusersInfo findById(Long id) {
        return signinusersInfoDao.selectByPrimaryKey(id);
    }
	public SigninusersInfo findByUsername(String  username) { return signinusersInfoDao.selectByUsername(username); }
    public List<SigninusersInfoVo> findAll() {
        return signinusersInfoDao.findByNames("all");
    }

    public PageInfo<SigninusersInfoVo> findPage(String names, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
		
		List<SigninusersInfoVo> all=null;
        Account user = (Account) request.getSession().getAttribute("user");
        if (user.getLevel().equals("管理员")) {all = signinusersInfoDao.findByNames(names);}
		
        
        return PageInfo.of(all);
    }
	
	public PageInfo<SigninusersInfoVo> findPageqt(String names, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
		
		List<SigninusersInfoVo> all=null;
        Account user = (Account) request.getSession().getAttribute("user");
        all = signinusersInfoDao.findByNames(names);
		
	
	
        
        return PageInfo.of(all);
    }


    public SigninusersInfo findByBianhao(String zhanghao) {
        return signinusersInfoDao.selectByUsername(zhanghao);
    }
   // public SigninusersInfoVo findByUserName(String name) {
//        return signinusersInfoDao.findByUsername(name);
//    }

	public SigninusersInfo login(String username, String mima) {
        SigninusersInfo signinusersInfo = signinusersInfoDao.findByUsernamezj(username);
        if (signinusersInfo == null) {
            throw new CustomException(ResultCode.USER_ACCOUNT_ERROR);
        }
        if (!SecureUtil.md5(mima).equalsIgnoreCase(signinusersInfo.getMima())) {
            throw new CustomException(ResultCode.USER_ACCOUNT_ERROR);
        }
        return signinusersInfo;
    }
	
    public void changeStatus(Long id) {
        SigninusersInfo signinusersInfo = signinusersInfoDao.selectByPrimaryKey(id);
        if(signinusersInfo.getStatus().equals("是")){
            signinusersInfo.setStatus("否");
            signinusersInfoDao.updateByPrimaryKey(signinusersInfo);
        }else if(signinusersInfo.getStatus().equals("否")){
            signinusersInfo.setStatus("是");
            signinusersInfoDao.updateByPrimaryKey(signinusersInfo);
        }
    }
	//ddaizdhifu
	//youtixing
	 public void update2(SigninusersInfoVo signinusersInfo) {
        List<Long> picflst = signinusersInfo.getPicflst();
        if (!CollectionUtil.isEmpty(picflst)) {
            signinusersInfo.setPic(picflst.toString());
        }
		
		        //shangxchxuantupxian
        if (StringUtils.isEmpty(signinusersInfo.getMima())) {
            // 默认密码123456
            signinusersInfo.setMima(SecureUtil.md5("123456"));
        } else {
            signinusersInfo.setMima(SecureUtil.md5(signinusersInfo.getMima()));
        }
        signinusersInfoDao.updateByPrimaryKeySelective(signinusersInfo);
    }
	
	
	
	
	public boolean changeMima(String username, String newMima,String ymm) {
    SigninusersInfo signinusersInfo = findByUsername(username);
    if(signinusersInfo == null) { throw new CustomException(ResultCode.USER_NOT_EXIST_ERROR); }
    else if(!SecureUtil.md5(ymm).equalsIgnoreCase(signinusersInfo.getMima())) { throw new CustomException(ResultCode.USER_ACCOUNT_ERROR);}
    signinusersInfo.setMima(SecureUtil.md5(newMima));
    update(signinusersInfo);
    return true;
    }


}
